package com.feifan.monthhelper.utils;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.feifan.monthhelper.constant.Constant;

import java.util.Calendar;
import java.util.List;

import cn.qqtheme.framework.picker.OptionPicker;
import cn.qqtheme.framework.util.LogUtils;
import cn.qqtheme.framework.widget.WheelView;

public class CommonDialogUtils {

    public static void ShowOptionPicker(Activity activity, List<String> items, OptionPicker.OnOptionPickListener listener) {
        ShowOptionPicker(activity, "", items, listener);
    }

    public static void ShowOptionPicker(Activity activity, String title, List<String> items, OptionPicker.OnOptionPickListener listener) {
        ShowOptionPicker(activity, title, items, "", listener);
    }

    public static void ShowOptionPicker(Activity activity, List<String> items, String selectItem, OptionPicker.OnOptionPickListener listener) {
        ShowOptionPicker(activity, "", items, selectItem, listener);
    }

    public static void ShowOptionPicker(Activity activity, String title, List<String> items, String selectItem, OptionPicker.OnOptionPickListener listener) {
        OptionPicker picker = new OptionPicker(activity, items);
        picker.setCanceledOnTouchOutside(false);
        picker.setDividerRatio(WheelView.DividerConfig.FILL);
        picker.setTitleText(title);
        picker.setSelectedItem(selectItem);
        picker.setCycleDisable(true);
        picker.setTextSize(18);
        picker.setOnOptionPickListener(listener);
//        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
//            @Override
//            public void onOptionPicked(int index, String item) {
//                LogUtils.debug("index=" + index + ", item=" + item);
//            }
//        });
        picker.show();
    }

    public static void showDateSelectDialog(String dateString, AppCompatActivity activity, OnTimeSelectListener onTimeSelectListener) {
//        String dateString = "";
        Calendar selectedDate = getCalendarFrom(dateString);

        TimePickerView pvTime = new TimePickerBuilder(activity, onTimeSelectListener)
                .setType(new boolean[]{true, true, true, false, false, false})//分别对应年月日时分秒，默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setContentTextSize(18)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
//                .setTitleText("Title")//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
//                .setTitleColor(Color.BLACK)//标题文字颜色
//                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
//                .setCancelColor(Color.BLUE)//取消按钮文字颜色
//                .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
//                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
//                .setRangDate(Calendar.getInstance().get(Calendar.YEAR) - 20, Calendar.getInstance().get(Calendar.YEAR) + 20)//默认是1900-2100年
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
//                .setRangDate(startDate,endDate)//起始终止年月日设定
                .setLabel("年","月","日","时","分","秒")
                .isDialog(false)//是否显示为对话框样式
                .build();
        pvTime.show();
    }


    /**
     * 日期字符串转date
     *
     * @param dateString 目标日期字符串
     * @return data
     */
    private static Calendar getCalendarFrom(String dateString) {
        Calendar selectedDate = Calendar.getInstance();
        if (!TextUtils.isEmpty(dateString)) {
            String[] strs = dateString.split(Constant.TIME_SPARATOR);
            if (strs.length > 0) {
                selectedDate.set(NumberUtils.StringToInt(strs[0]), selectedDate.get(Calendar.YEAR));
            }
            if (strs.length > 1) {
                selectedDate.set(NumberUtils.StringToInt(strs[1]), selectedDate.get(Calendar.MONTH));
            }
            if (strs.length > 2) {
                selectedDate.set(NumberUtils.StringToInt(strs[2]), selectedDate.get(Calendar.DAY_OF_MONTH));
            }
        }
        return selectedDate;
    }
}
